package cn.yh.shop.product.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.yh.shop.product.model.Category;
import cn.yh.shop.product.model.Goods;
import cn.yh.shop.product.service.impl.CategoryServiceImpl;
import cn.yh.shop.product.service.impl.GoodsServiceImpl;
/**
 * 
 * @Description:类别处理
 * @author:"陈泓源"
 * @time:2016年12月14日 下午5:53:44
 */
public class CategoryAction {
	private static Logger logger = LogManager.getLogger(CategoryAction.class);
	@Autowired
	private CategoryServiceImpl categoryService;
	@Autowired
	private GoodsServiceImpl goodsService;
	
	private HttpServletRequest request = ServletActionContext.getRequest();
	private HttpServletResponse response = ServletActionContext.getResponse();
	private HttpSession session = request.getSession();
	/**
	 * 获取三级分类
	 * @return
	 */
	public String getFirstCate(){
		response.setCharacterEncoding("utf-8");
		
		//一级分类容器
		List<Category> plist = categoryService.getFirstCate();
		//得到一级分类
		session.setAttribute("pcate", plist);
		
		//二级分类容器
		List<List<Category>> list = new ArrayList<List<Category>>();
		
		
		//根据一级分类获取二级分类
		for (Category category : plist) {
			Category cate =new Category();
			//获取父类id
			cate.setPid(category.getId());
			//根据id查询数据
			list.add(categoryService.getNextCate(cate));
		}
		//得到二级分类
		session.setAttribute("catelist", list);
		//获取三级分类
		List<List<Category>> tlist = new ArrayList<List<Category>>();
		for (List<Category> list2 : list) {
			for (Category category : list2) {
				Category categ = new Category();
				categ.setPid(category.getId());
				tlist.add(categoryService.getNextCate(categ));
			}
		}
		//获取商品
		List<Goods> goodslist = new ArrayList<Goods>();
		List<Category> thlist = new ArrayList<Category>();
		for (List<Category> list2 : tlist) {
			for (Category category : list2) {
				//logger.info(category.getName());
				thlist.add(category);
				HashMap<String,Object> map =new HashMap<String,Object>();
				map.put("cateid", category.getId());
				//商品集
				List<Goods> glist = goodsService.getGoodsByCondi(map);
				for (Goods goods : glist) {
					logger.info("商品名称:"+goods.getName());
					goodslist.add(goods);
				}
				
			}
		}
		session.setAttribute("tlist", thlist);
//		for (Goods goods : goodslist) {
//			logger.info(goods.getName());
//		}
//		List<List<Goods>> goodslist = new ArrayList<List<Goods>>();
//		for (Category list2 : plist) {
//			Category categ = new Category();
//			//设置查询条件
//			categ.setPid(list2.getId());
//			//查询二级分类 	li存放二级分类
//			List<Category> li = this.getNextCate(categ);
//			if(li.size()!=0){
//				//遍历二级分类 
//				for (Category category : li) {
//					//根据得到的二级分类获取对应的三级分类
//					categ.setPid(category.getId());
//					//获取三级分类
//					li = this.getNextCate(categ);
//					//根据三级分类查询商品
//					List<Goods> goods = new ArrayList<Goods>();
//					if(li.size()!=0){
//						//遍历三级分类
//						for (Category categor : li) {
//							HashMap<String,Integer> map = new HashMap<String,Integer>();
//							map.put("cateid", categor.getId());
//							goods.add(goodsservice.getGoodsByCondit(map));
//						}
//						goodslist.add(goods);
//						goods = new ArrayList<Goods>();
//					}else{
//						goods.add(new Goods());
//						goodslist.add(goods);
//					}
//				}
//				reslist.add( goodslist);
//				//清除所有元素 不能使用clear()
//				goodslist = new ArrayList<List<Goods>>();
//			}else{
//				//不可直接设置null
//				goodslist.add(new ArrayList<Goods>());
//				reslist.add(goodslist);
//			}
//		}
		session.setAttribute("tcatelist", goodslist);
		List<Category> firstlist=categoryService.getFirstCate();
		List<Category> secondlist=categoryService.getSecondCate();
		List<Category> thirdlist=categoryService.getThirdCate(null);
		session.setAttribute("firstlist", firstlist);
		session.setAttribute("secondlist", secondlist);
		session.setAttribute("thirdlist", thirdlist);
		return "success";
	}
	/**
	 * 获取下级分类
	 * @param cate 类别对象
	 * @return 返回要查询的级别的数据
	 */
	public List<Category> getNextCate(Category cate){
		response.setCharacterEncoding("utf-8");
		if(cate.getPid()==null){
			List<String> list = getPrameter("id");
			cate = new Category();
			cate.setPid(Integer.valueOf(list.get(0)));
		}
		List<Category> list = categoryService.getNextCate(cate);
		return list;
	}
	/**
	 * 获取表单传输过来的参数
	 * @param pra
	 * @return
	 */
	public List<String> getPrameter(String... pra){
		List<String> list = new ArrayList<String>();
		for (String string : pra) {
			//获取传输的参数
			list.add(request.getParameter(string));
		}
		return list;
	}
	/**
	 * 
	 * @Description:获取首页一级分类
	 * void
	 * @exception:
	 * @author: "李雨"
	 * @throws IOException 
	 * @time:2016年12月21日 下午3:02:39
	 */
}
